import time

import typer
import sys

sys.path.append("../")  # noqa
from task_utils.task_alumni_rank_score import job_person_score
from task_utils.task_es import score_alumni_by_redis

from task_utils.task_alumni_completion import task_update_alumni_completion_score
from datetime import datetime, timedelta
from api.dataapi.task import task_alumni_belongs_permission, task_alumni_join_act, task_activity_institution, \
    task_activity_contacts

from config import col_activity, col_alumni

app = typer.Typer()


@app.command()
def alumni_permission(before: int = typer.Option(None, help="更新N天的数据")):
    """从活动表的 *主协办* 到alumni表, 作为部门数据权限."""
    query = {"is_deleted": {"$ne": True}}
    if before:
        before_days = datetime.utcnow() - timedelta(days=before)
        query["update_time"] = {"$gte": before_days}

    persons = col_alumni.find(query).distinct("_id")
    with typer.progressbar(persons) as progress:
        for user in progress:
            person_id = str(user)
            task_alumni_belongs_permission(person_id)


@app.command()
def alumni_join_act(before: int = typer.Option(None, help="更新N天的数据")):
    """计算人参与的活动次数"""
    query = {"is_deleted": {"$ne": True}}
    if before:
        before_days = datetime.utcnow() - timedelta(days=before)
        query["update_time"] = {"$gte": before_days}

    persons = col_alumni.find(query).distinct("_id")
    with typer.progressbar(persons) as progress:
        for user in progress:
            person_id = str(user)
            task_alumni_join_act(person_id)


@app.command()
def act_inst(before: int = typer.Option(None, help="更新N天的数据")):
    """主协办的机构名称 插入到活动 cache"""
    query = {"is_deleted": {"$ne": True}}
    if before:
        before_days = datetime.utcnow() - timedelta(days=before)
        query["update_time"] = {"$gte": before_days}
    activities = col_activity.find(query).distinct("_id")
    with typer.progressbar(activities) as progress:
        for activity in progress:
            activity_id = str(activity)
            task_activity_institution(activity_id)


@app.command()
def act_contacts(before: int = typer.Option(None, help="更新N天的数据")):
    """更新活动的参与人次"""
    query = {"is_deleted": {"$ne": True}}
    if before:
        before_days = datetime.utcnow() - timedelta(days=before)
        query["update_time"] = {"$gte": before_days}
    activities = col_activity.find(query).distinct("_id")
    with typer.progressbar(activities) as progress:
        for activity in progress:
            activity_id = str(activity)
            task_activity_contacts(activity_id)


@app.command()
def task_alumni_completion(before: int = typer.Option(None, help="更新N天的数据")):
    """更新人的得分完整度"""
    query = {"is_deleted": {"$ne": True}}
    if before:
        before_days = datetime.utcnow() - timedelta(days=before)
        query["update_time"] = {"$gte": before_days}
    alumni = col_alumni.find(query).distinct("_id")

    with typer.progressbar(alumni) as progress:
        for person in progress:
            task_update_alumni_completion_score(str(person))


@app.command()
def task_person_score():
    """计算个人得分"""
    for p in col_alumni.find({"is_deleted": {"$ne": True}}):
        job_person_score(str(p["_id"]))


@app.command()
def task_score_alumni_by_redis():
    """
    必须先执行task-person-score cli
     排序的得分写入redis,计算如下字段
    prank_alumni_score
    rank_alumni_score
    """
    score_alumni_by_redis()


if __name__ == "__main__":
    app()
